package com.yiwei.hdb.util;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.yiwei.hdb.adapter.RepayRecordNewAdapter;
import com.yiwei.hdb.adapter.base.ItemAdapter;
import com.yiwei.hdb.constant.Const;
import com.yiwei.hdb.model.RepayRecordModel;

public abstract class OnSecLoadeMoreListener extends RecyclerView.OnScrollListener {

    private int lastVisibleItem;
    private int pastDuePageIndex = 1;
    private ItemAdapter adapter;

    public OnSecLoadeMoreListener(ItemAdapter adapter) {
        this.adapter = adapter;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        lastVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition();
    }


    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == 0) {//停止滚动

        }
        try {
            if (newState == RecyclerView.SCROLL_STATE_IDLE
                    && lastVisibleItem + 1 == adapter.getItemCount()) {
                onLoadMore(++pastDuePageIndex);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setPage(int page) {
        pastDuePageIndex = page;
    }

    public abstract void onLoadMore(int page);
}